var discountDAO = require('../models/discountDAO')
var formidable = require('formidable')
var path = require('path')

// const path = require('path');
var userDAO = require('../models/userDAO')
var jwt = require('jsonwebtoken')
var bcrypt = require('bcrypt')
var discountController = {
    getAllDiscount: function (req, res) {
        discountDAO.getAllDiscount(function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
              
                res.json({ code: 200, data: results, msg: '查询成功' })
                  console.log(res.data)
            }
        })
    },
    getDiscountID: function (req, res) {
        var discountID = req.query.discountID
        discountDAO.getDiscountID(discountID, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    getFind: function (req, res) {
        var dis_Name = '%' + req.query.disName + '%'
        discountDAO.getFind(dis_Name, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    // 在分类 商品中搜索
    getclassFind: function (req, res) {

        var classID = req.query.classID
        console.log(classID)
        var dis_Name = '%' + req.query.disName + '%'
        discountDAO.getclassFind([classID, dis_Name], function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    getTime: function (req, res) {
        var tradeID = req.query.tradeID
        discountDAO.getTime(tradeID, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
                if (results == null || results.length == 0) {
                    res.json({ code: 200, msg: '找不到数据,查询失败' })
                } else {
                    res.json({ code: 200, data: results, msg: '查询成功！' })
                }
            }
        })
    },
    getdisClass: function (req, res) {
        var classID = req.query.classID
        discountDAO.getdisClass(classID, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '查询失败' })
            } else {
                res.json({ code: 200, data: results, msg: '查询成功！' })
            }
        })
    },
    upImages: function (req, res) {
        var results = {
            "errno": 0,
            "data": []
        }
        var form = new formidable.IncomingForm()
        form.uploadDir = path.join(__dirname, '..', 'public/upload')
        form.keepExtensions = true
        form.on('file', function (err, file) {

            results.data.push(path.parse(file.path).base)
            console.log('数组', results.data)
        }

        )

        form.parse(req, function (err, fields, files) {
            let num = results.data.length
            if (num > 10) {
                res.status(500).json({ msg: '上传的图片不能大于10张' })
            } else {
                // var disimages = []; 

                // for(var i=0;i<results.data.length;i++) {
                //     disimages[i] = results.data[i];
                // }

                var disimages = []
                for (let i = 0; i < num; i++) {
                    disimages[i] = results.data[i]
                }
                console.log('数组1', disimages)
                var disimage1 = disimages[0]
                var disimage2 = disimages[1]
                var disimage3 = disimages[2]
                var disimage4 = disimages[3]
                var disimage5 = disimages[4]
                var disimage6 = disimages[5]
                var disimage7 = disimages[6]
                var disimage8 = disimages[7]
                var disimage9 = disimages[8]
                var disimage10 = disimages[9]
            }
            if (err) {
                res.send('图片上传错误')
            }

            var discount = { discountID: fields.discountID, disimage1: disimage1, disimage2: disimage2, disimage3: disimage3, disimage4: disimage4, disimage5: disimage5, disimage6: disimage6, disimage7: disimage7 ,disimage8: disimage8, disimage9: disimage9, disimage10: disimage10 }
            // {discountID:fields.discountID,disimages:disimages}
            console.log('再次测试', discount)
            // var discountID = fields.discountID
            // var disimages = path.parse(files.psimg.path).base 
            discountDAO.upImages(discount, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '上传失败' })
                } else {
                    res.json({ code: 200, data: results, msg: '上传成功！' })
                }
            })



        })
    },


}
module.exports = discountController